package com.blvtphan.tipocketclubs.gui;

import java.util.ArrayList;
import java.util.List;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.ListView;

import com.blvtphan.tipocketclubs.R;
import com.blvtphan.tipocketclubs.abstracts.AbstractActivity;
import com.blvtphan.tipocketclubs.adapter.TrackerDetailAdapter;
import com.blvtphan.tipocketclubs.api.ApiConstant;
import com.blvtphan.tipocketclubs.api.ApiHelper;
import com.blvtphan.tipocketclubs.custom.CustomTextView;
import com.blvtphan.tipocketclubs.model.TrackerDetailModel;
import com.blvtphan.tipocketclubs.model.TrackerSummaryModel;
import com.blvtphan.tipocketclubs.system.UserSetting;
import com.blvtphan.tipocketclubs.util.ConnectionUtils;
import com.blvtphan.tipocketclubs.util.Constants.APIConstant;

public class TrackerSummaryActivity extends AbstractActivity{
	
	private ListView mLstViewTrackerDetail;
	private TrackerDetailAdapter adapter;
	private List<TrackerDetailModel> lstTrackerDetail;
	private TrackerSummaryModel trackerSummary;
	private CustomTextView tvTotalSpent;
	private CustomTextView tvTotalSaved;
	private CustomTextView tvTotalUsed;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		
		getDataFromServer();
	}
	
	/**
	 * get all tracker data from server
	 */
	private void getDataFromServer() {
		lstTrackerDetail = new ArrayList<TrackerDetailModel>();
		trackerSummary = new TrackerSummaryModel();
		
		String urlDetail = ApiHelper.genAPI(ApiConstant.API.API_GET_TRACKER_DETAIL);
		// Fill PlanId and UserId on Url
		urlDetail = urlDetail.replace("{0}", UserSetting.getInstance().getToken());
		ConnectionUtils.ExcuteHttpGetThread excuteHttpGetDetail = new ConnectionUtils.ExcuteHttpGetThread(
				mContext, mProcessGetTrackerDetail, urlDetail, false);
		excuteHttpGetDetail.execute();
		
		String url = ApiHelper.genAPI(ApiConstant.API.API_GET_TRACKER_SUMMARY);
		// Fill PlanId and UserId on Url
		url = url.replace("{0}", UserSetting.getInstance().getToken());
		ConnectionUtils.ExcuteHttpGetThread excuteHttpGet = new ConnectionUtils.ExcuteHttpGetThread(
				mContext, mProcessGetTrackerSummary, url, false);
		excuteHttpGet.execute();
		
	}
	
	/**
	 * Handler of get merchants
	 */
	private Handler mProcessGetTrackerSummary = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case APIConstant.HANDLER_REQUEST_FAIL:
				break;
			case APIConstant.HANDLER_REQUEST_SUCCESS:
				String data = (String) msg.obj;

				trackerSummary = ApiHelper.parseJSONGetTrackerSummary(
						mContext, data);
				if (trackerSummary != null) {
					initSummaryLayout();
				} else {

				}

				break;
			default:
				break;
			}
		}
	};
	
	/**
	 * Handler of get merchants
	 */
	private Handler mProcessGetTrackerDetail = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case APIConstant.HANDLER_REQUEST_FAIL:
				break;
			case APIConstant.HANDLER_REQUEST_SUCCESS:
				String data = (String) msg.obj;

				lstTrackerDetail = ApiHelper.parseJSONGetTrackerDetail(
						mContext, data);
				if (lstTrackerDetail != null) {
					initDetailLayout();
				} else {

				}

				break;
			default:
				break;
			}
		}
	};
	
	private void initSummaryLayout() {
		tvTotalSpent.setText(trackerSummary.getSpent());
		tvTotalSaved.setText(trackerSummary.getSaved());
		tvTotalUsed.setText(trackerSummary.getUsed());
		
	}
	
	private void initDetailLayout() {
		adapter = new TrackerDetailAdapter(mContext, lstTrackerDetail);
		mLstViewTrackerDetail.setAdapter(adapter);
	}
	
	@Override
	public int getContentViewId() {
		return R.layout.activity_clubs_tracker;
	}

	@Override
	public void initView() {
		mLstViewTrackerDetail = (ListView)findViewById(R.id.lst_tracker_detail);
		tvTotalSpent = (CustomTextView)findViewById(R.id.txt_summary_spent);
		tvTotalSaved = (CustomTextView)findViewById(R.id.txt_summary_saved);
		tvTotalUsed = (CustomTextView)findViewById(R.id.txt_summary_used);
	}

	@Override
	public String getTitleBarTitle() {
		return mContext.getString(R.string.title_club_tracker);
	}
	
}
